package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.DeptUtil;
import com.central.app.service.FlowCommonService;
import com.central.app.service.HandlerService;
import com.central.app.service.TreeService;
import com.central.apps.common.util.MapCustomUtils;
import com.central.apps.service.*;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.web.PageResult;
import com.central.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.JSONUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理项目年度业务逻辑
 *
 * @author lidachao
 * @date 2023/4/10
 */
@Component
@Slf4j
public class ProjectMyTaskHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private TreeService treeService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("getScheduleFill".equals(type)) {
            Integer proId = MapUtils.getInteger(params, "proid");
            Integer taskId = MapUtils.getInteger(params, "taskid");

            params.remove("taskid");

            Map<String, Object> treeParams = new HashMap<>(4);
            treeParams.put("tree_id", "id");
            treeParams.put("tree_pid", "parentId");
            treeParams.put("proid", proId);
            treeParams.put("parent_filter", "id=" + taskId);
            List<Map<String, Object>> taskList = treeService.findTreeData("mytaskApp", treeParams);

            if (CollectionUtils.isNotEmpty(taskList)) {
                StringBuilder sb = new StringBuilder();
                for (Map<String, Object> taskMap : taskList) {
                    sb.append(MapUtils.getInteger(taskMap, "id")).append(",");
                }
                sb.deleteCharAt(sb.length() - 1);

                // 查询执行单数据
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_schedulefill where proid = " + proId + " and taskid in (" + sb.toString() + ")");

                JSONObject json = new JSONObject();
                json.put("code", "0");
                json.put("count", list.size());
                json.put("data", list);
                return json;
            }
        }
        if ("getTaskDeli".equals(type)) {
            Integer proId = MapUtils.getInteger(params, "proid");
            Integer taskId = MapUtils.getInteger(params, "taskid");

            params.remove("taskid");

            Map<String, Object> treeParams = new HashMap<>(4);
            treeParams.put("tree_id", "id");
            treeParams.put("tree_pid", "parentId");
            treeParams.put("proid", proId);
            treeParams.put("parent_filter", "id=" + taskId);
            List<Map<String, Object>> taskList = treeService.findTreeData("mytaskApp", treeParams);

            if (CollectionUtils.isNotEmpty(taskList)) {
                StringBuilder sb = new StringBuilder();
                for (Map<String, Object> taskMap : taskList) {
                    sb.append(MapUtils.getInteger(taskMap, "taskid")).append(",");
                }
                sb.deleteCharAt(sb.length() - 1);

                // 查询交付物数据
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_taskdeliverables where proid = " + proId + " and taskid in (" + sb.toString() + ")");

                JSONObject json = new JSONObject();
                json.put("code", "0");
                json.put("count", list.size());
                json.put("data", list);
                return json;
            }
        }
        if ("getManHour".equals(type)) {
            Integer proId = MapUtils.getInteger(params, "proid");
            Integer taskId = MapUtils.getInteger(params, "taskid");

            params.remove("taskid");

            Map<String, Object> treeParams = new HashMap<>(4);
            treeParams.put("tree_id", "id");
            treeParams.put("tree_pid", "parentId");
            treeParams.put("proid", proId);
            treeParams.put("parent_filter", "id=" + taskId);
            List<Map<String, Object>> taskList = treeService.findTreeData("mytaskApp", treeParams);

            if (CollectionUtils.isNotEmpty(taskList)) {
                StringBuilder sb = new StringBuilder();
                for (Map<String, Object> taskMap : taskList) {
                    sb.append(MapUtils.getInteger(taskMap, "taskid")).append(",");
                }
                sb.deleteCharAt(sb.length() - 1);

                // 查询交付物数据
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_manhourfilldtl where proid = " + proId + " and taskid in (" + sb.toString() + ")");

                JSONObject json = new JSONObject();
                json.put("code", "0");
                json.put("count", list.size());
                json.put("data", list);
                return json;
            }
        }
        if ("getProInfo".equals(type)) {
            LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
            String userName = loginUser.getUsername();
            String deptCode = loginUser.getDeptcode();

            String deptStr = "";
            if (!"admin".equals(userName)) {
                deptStr = DeptUtil.getDeptString(deptCode);
            }

            StringBuilder proStr = new StringBuilder();
            if (StringUtils.isNotEmpty(deptStr)) {
                // 查询部门下全部人员信息
                StringBuilder personStr = new StringBuilder();
                List<Map<String, Object>> personList = handlerService.findBySql("select id from gq_person where deptcode in (" + deptStr + ")");

                // 组织人员ID字符串
                if (CollectionUtils.isNotEmpty(personList)) {
                    for (Map<String, Object> personMap : personList) {
                        Integer personId = MapUtils.getInteger(personMap, "id");
                        personStr.append(personId).append(",");
                    }
                }

                if (StringUtils.isNotEmpty(personStr)) {
                    personStr.deleteCharAt(personStr.length() - 1);
                    List<Map<String, Object>> taskList = handlerService.findBySql("select proid from gq_mytask where manager in (" + personStr + ")");

                    if (CollectionUtils.isNotEmpty(taskList)) {
                        for (Map<String, Object> taskMap : taskList) {
                            Integer proId = MapUtils.getInteger(taskMap, "proid");

                            if (!proStr.toString().contains("、" + proId + "、")) {
                                proStr.append(proId).append("、");
                            }
                        }
                    }
                }
            }

            Map<String, Object> searchParams = new HashMap<>();
            searchParams.putAll(params);
            if (StringUtils.isNotEmpty(proStr)) {
                proStr.deleteCharAt(proStr.length() - 1);
                searchParams.put("id_where_in", proStr.toString());
            }

            // 查询项目信息
            PageResult<Map<String, Object>> list = handlerService.findPage("proaccountApp", "gq_proaccount", searchParams);
            return list;
        }
        if ("getTaskInfo".equals(type)) {
            Integer proId = MapUtils.getInteger(params, "proid");
            LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
            String userName = loginUser.getUsername();
            String deptCode = loginUser.getDeptcode();

            StringBuilder deptStr = new StringBuilder();

            if (!"admin".equals(userName)) {
                // 查询组织树
                Map<String, Object> treeParams = new HashMap<>(4);
                treeParams.put("tree_id", "code");
                treeParams.put("tree_pid", "pcode");
                treeParams.put("tableName", "sys_dept");
                treeParams.put("parent_filter", "code='" + deptCode + "'");
                List<Map<String, Object>> deptList = treeService.findTreeData(treeParams);

                // 组织部门编码字符串
                if (CollectionUtils.isNotEmpty(deptList)) {
                    for (Map<String, Object> deptMap : deptList) {
                        String tempDeptCode = MapUtils.getString(deptMap, "code");
                        deptStr.append("'").append(tempDeptCode).append("',");
                    }
                }
            }

            StringBuilder personStr = new StringBuilder();
            if (StringUtils.isNotEmpty(deptStr.toString())) {
                deptStr.deleteCharAt(deptStr.length() - 1);

                // 查询部门下全部人员信息
                List<Map<String, Object>> personList = handlerService.findBySql("select id from gq_person where deptcode in (" + deptStr + ")");

                // 组织人员ID字符串
                if (CollectionUtils.isNotEmpty(personList)) {
                    for (Map<String, Object> personMap : personList) {
                        Integer personId = MapUtils.getInteger(personMap, "id");
                        if (!personStr.toString().contains("、" + personId + "、")) {
                            personStr.append(personId).append("、");
                        }
                    }
                }
            }

            Map<String, Object> searchParams = new HashMap<>();
            searchParams.putAll(params);
            if (StringUtils.isNotEmpty(personStr)) {
                personStr.deleteCharAt(personStr.length() - 1);
                searchParams.put("manager_where_in", personStr.toString());
            }

            // 查询项目信息
            PageResult<Map<String, Object>> list = handlerService.findPage("mytaskApp", "gq_mytask", searchParams);
            return list;
        }
        return null;
    }
}
